package servlets;

import java.io.IOException;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import delegate.BusinessDelegate;
import dtos.ArticuloDTO;

/**
 * Servlet implementation class Busqueda
 */
@WebServlet("/Busqueda")
public class Busqueda extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Busqueda() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String valor = request.getParameter("valor");//el valor del search
		String tipo = request.getParameter("tipo"); //ninio?electrodomestico?
		String operacion = request.getParameter("operacion"); //compra o listado
		String resp = "";
		
		try {
			List<ArticuloDTO> lista = BusinessDelegate.getInstancia().busqueda(valor,tipo);
			
			if(operacion.equals("compra")){
				
				if(tipo.equalsIgnoreCase("ninio")){
					for(ArticuloDTO art: lista)
						resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
							"<td>" + art.getDescripcion() + "</td>"+
							"<td>" + art.getMarca() + "</td>"+
							"<td>" + art.getPrecio() + "</td>"+
							"<td> <img src=\"" + art.getFotoURL() + "\" >  </td> " +
							"<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
							"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
							"<button type=\"button\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "') \">" +
							"<img src=\"img/agregar.png\" /> </button> </td> </tr>" ;
						
				}else if(tipo.equalsIgnoreCase("moda")){
					for(ArticuloDTO art: lista)
						resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
								"<td>" + art.getDescripcion() + "</td>"+
								"<td>" + art.getMarca() + "</td>"+
								"<td>" + art.getPrecio() + "</td>"+
								"<td>" + art.getColor()+ "</td> " +
								"<td>" + art.getTalle()+ "</td> " +	
								"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " +
								"<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
								"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
								"<button type=\"button\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "') \">" +
								"<img src=\"img/agregar.png\" /> </button> </td> </tr>" ;
				}if(tipo.equalsIgnoreCase("electrodomestico")){
					for(ArticuloDTO art: lista)
						resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
								"<td>" + art.getDescripcion() + "</td>"+
								"<td>" + art.getMarca() + "</td>"+
								"<td>" + art.getPrecio() + "</td>"+
								"<td>" + art.getFichaTecnica()+ "</td> " +
								"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " +
								"<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
								"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
								"<button type=\"button\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "')\" style=\"border: 0; background: transparent\">" +
								"<img src=\"img/agregar.png\" /> </button> </td> </tr>" ;		 

				}if(tipo.equalsIgnoreCase("mueble")){
					for(ArticuloDTO art: lista)
						if(art.getTipo().equals("Mueble"))
							resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
									"<td>" + art.getDescripcion() + "</td>"+
									"<td>" + art.getMarca() + "</td>"+
									"<td>" + art.getPrecio() + "</td>"+
									"<td>" + art.getFichaTecnica()+ "</td> " +
									"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " +
									"<td align=\"center\"> <input id=\""+ art.getCodigoArticulo() +"\" size=\"1\" name=\"cantidad\" " +   
									"style=\"width: 30px;\" onchange=\"validar(this)\" />" +
									"<button type=\"button\" style=\"border: 0; background: transparent\" style=\"border: 0; background: transparent\" onclick=\"agregarItemCarrito('" + art.getCodigoArticulo() + "')\">" +
									"<img src=\"img/agregar.png\" /> </button> </td> </tr>" ;		 
				}
						 
			}else if(operacion.equals("listado")){
				
				if(tipo.equalsIgnoreCase("ninio")){
					for(ArticuloDTO art: lista)
						resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
							"<td>" + art.getDescripcion() + "</td>"+
							"<td>" + art.getMarca() + "</td>"+
							"<td>" + art.getPrecio() + "</td>"+
							"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\">  </td> ";
						
				}else if(tipo.equalsIgnoreCase("moda")){
					for(ArticuloDTO art: lista)
						resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
								"<td>" + art.getDescripcion() + "</td>"+
								"<td>" + art.getMarca() + "</td>"+
								"<td>" + art.getPrecio() + "</td>"+
								"<td>" + art.getColor()+ "</td> " +
								"<td>" + art.getTalle()+ "</td> " +
								"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;
				}if(tipo.equalsIgnoreCase("electrodomestico")){
					for(ArticuloDTO art: lista)
						resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
								"<td>" + art.getDescripcion() + "</td>"+
								"<td>" + art.getMarca() + "</td>"+
								"<td>" + art.getPrecio() + "</td>"+
								"<td>" + art.getFichaTecnica()+ "</td> "+
								"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;

				}if(tipo.equalsIgnoreCase("mueble")){
					for(ArticuloDTO art: lista)
						if(art.getTipo().equals("Mueble"))
							resp += "<tr> <td>" + art.getCodigoArticulo() + "</td>" +
									"<td>" + art.getDescripcion() + "</td>"+
									"<td>" + art.getMarca() + "</td>"+
									"<td>" + art.getPrecio() + "</td>"+
									"<td>" + art.getFichaTecnica()+ "</td> "+
									"<td> <img src=\"" + art.getFotoURL() + "\" style=\"width:100px; height:100px;\" >  </td> " ;
				}
				
			}
			response.getWriter().write(resp);
			
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

}
